package io.lvdaxian.mybatis10.type;

import io.lvdaxian.mybatis10.session.Configuration;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 实现 基础抽象的 类型处理事件
 *
 * @author lihh
 */
public abstract class BaseTypeHandler<T> implements TypeHandler<T> {
  
  protected Configuration configuration;
  
  public void setConfiguration(Configuration configuration) {
    this.configuration = configuration;
  }
  
  @Override
  public void setParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
    // 定义抽象方法，由子类实现不同类型的属性设置
    setNonNullParameter(ps, i, parameter, jdbcType);
  }
  
  protected abstract void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException;
  
  @Override
  public T getResult(ResultSet rs, String columnName) throws SQLException {
    return getNullableResult(rs, columnName);
  }
  
  protected abstract T getNullableResult(ResultSet rs, String columnName) throws SQLException;
}
